package cn.zxy.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle方法处理器控制器(controller)方法调用之前");

        //jwt说你可以把请求的数据 放到header头里面来进行接收
        String token = request.getHeader("token");
        //jwt说你可以把请求的数据 放到参数上来进行接收
//        String token = request.getParameter("token");
        if (token != null && token.equals("206")){
            return true;
        }
        System.out.println("不是");

        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("{\"code\":\"401\",\"message\":\"token必须传入\"}");

//        return true;
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle方法控制器(controller)方法调用之后 在视图之前执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion在视图渲染之后执行");
    }
}
